package demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import demo.CustomException;
import demo.ExceptionEnum;
import demo.entity.SystemNormalUser;
import demo.mapper.SystemNormalUserMapper;
import demo.service.SystemNormalUserService;
import demo.vo.SystemNormalUserSaveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 普通用户表 服务实现类
 * </p>
 *
 * @author TangZibo
 * @since 2021-04-20
 */
@Service
public class SystemNormalUserServiceImpl extends ServiceImpl<SystemNormalUserMapper, SystemNormalUser> implements SystemNormalUserService {

    private GarnetRpcServiceImpl garnetRpcService;

    @Autowired
    public SystemNormalUserServiceImpl(GarnetRpcServiceImpl garnetRpcService) {
        this.garnetRpcService = garnetRpcService;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public SystemNormalUser registerNormalUser(SystemNormalUserSaveVo saveVo, SystemNormalUser systemNormalUser) {

        //保存注册企业用户信息
        boolean saveResult = this.save(systemNormalUser);
        if (!saveResult) {
            throw new CustomException("保存注册企业信息失败");
        }
        //调用garnet接口进行用户信息注册
        try {
            garnetRpcService.callGarnetRegisterNormalUser(saveVo);
        } catch (Exception e) {
            throw new CustomException("远程过程调用Garnet注册接口异常");
        }
        return systemNormalUser;
    }
}
